package org.ielts.action;

import static com.yifeng.struts2.ResultParameters.*;
import static com.yifeng.struts2.ResultConstants.*;
import static com.yifeng.struts2.ActionUtils.*;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.ielts.domain.base.Course;
import org.ielts.domain.base.SignCourse;
import org.ielts.domain.base.Student;
import org.ielts.domain.log.ChangeSignCourseLog;
import org.ielts.domain.log.ChangeSignCourseType;
import org.ielts.domain.log.ExtendSignCourseLog;
import org.ielts.domain.log.SignCourseLog;
import org.ielts.domain.security.User;
import org.ielts.service.ConsultantService;
import org.ielts.service.CourseService;
import org.ielts.service.SignCourseLogService;
import org.ielts.service.SignCourseService;
import org.ielts.service.StudentService;
import org.ielts.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;

import com.yifeng.modules.annotations.Operation;
import com.yifeng.utils.JsonResultBuilder;

public class SignCourseAction extends BaseAction {

	@Operation(result = FILL)
	public String changeSignCourse() {
		String id = form.readString("signCourseId");

		SignCourse signCourse = signCourseService.findOne(id);

		put("signCourse", signCourse);

		return FILL;
	}

	@Operation(result = JSON)
	public String doChangeSignCourse() {
		String id = form.readString("id");

		String type = form.readString("type");
		ChangeSignCourseType typeEnum = ChangeSignCourseType.Other;
		if (StringUtils.isNotBlank(type)) {
			typeEnum = Enum.valueOf(ChangeSignCourseType.class, type);
		}

		Date endTime = valueReader.name("endTime").defaultValue(null).dateValue("yyyy-MM-dd");
		String title = form.readString("title");
		int times = valueReader.name("times").defaultValue(0).intValue();

		ChangeSignCourseLog changeSignCourseLog = signCourseService.changeSignCourse(id, endTime, times, typeEnum,
				title);

		setJson(JsonResultBuilder.success(true).msg("修改课时成功").add("id", changeSignCourseLog.getStudent().getId())
				.json());

		return JSON;
	}

	@Operation(result = FILL)
	public String extendSignCourse() {
		String id = form.readString("signCourseId");

		Iterable<User> consultants = consultantService.listConsultant();
		put("consultants", consultants);

		SignCourse signCourse = signCourseService.findOne(id);
		put("signCourse", signCourse);

		return FILL;
	}

	@Operation(result = JSON)
	public String doExtendSignCourse() {
		String id = form.readString("id");

		String contractId = form.readString("contractId");
		String cardId = form.readString("cardId");
		Date endTime = valueReader.name("endTime").defaultValue(null).dateValue("yyyy-MM-dd");
		int times = valueReader.name("times").defaultValue(0).intValue();

		String consultant = form.readString("consultant");
		String title = form.readString("title");

		ExtendSignCourseLog extendSignCourseLog = signCourseService.extendSignCourse(id, contractId, cardId, endTime,
				times, consultant, title);

		setJson(JsonResultBuilder.success(true).msg("延长课时成功").add("id", extendSignCourseLog.getStudent().getId())
				.json());

		return JSON;
	}

	@Operation(result = FILL)
	public String modifySignCourse() {
		String id = form.readString("id");

		SignCourse signCourse = signCourseService.findOne(id);
		put("signCourse", signCourse);

		Iterable<Course> courses = courseService.findAll();
		put("courses", courses);

		put("begin", DateUtil.getAsString(DateUtil.now(), "yyyy-MM-dd"));
		put("end", DateUtil.getAsString(DateUtil.nextYear(), "yyyy-MM-dd"));

		return FILL;
	}

	@Operation(result = JSON)
	public String doModifySignCourse() {
		String id = form.readString("id");

		String courseId = form.readString("courseId");
		String contractId = form.readString("contractId");
		String cardId = form.readString("cardId");
		String term = form.readString("term");
		int times = valueReader.name("times").defaultValue(0).intValue();

		Date beginTime = valueReader.name("beginTime").defaultValue(null).dateValue("yyyy-MM-dd");
		Date endTime = valueReader.name("endTime").defaultValue(null).dateValue("yyyy-MM-dd");

		String lessonCard = form.readString("lessonCard");

		SignCourse signCourse = signCourseService.modifySignCourse(id, courseId, contractId, cardId, term, lessonCard,
				times, beginTime, endTime);

		setJson(JsonResultBuilder.success(true).msg("修改注册课程成功").add("id", signCourse.getStudent().getId()).json());

		return JSON;
	}

	@Autowired
	StudentService studentService;

	@Autowired
	CourseService courseService;

	@Autowired
	SignCourseService signCourseService;

	@Autowired
	ConsultantService consultantService;

	@Operation(result = FILL, operationText = "客户注册课程")
	public String signUp() {
		String id = form.readString("id");

		Student student = studentService.findOne(id);
		put("student", student);

		Iterable<Course> courses = courseService.findAll();
		put("courses", courses);

		Iterable<User> consultants = consultantService.listConsultant();
		put("consultants", consultants);

		put("now", new Date());

		return FILL;
	}

	@Operation(result = JSON)
	public String doSignCourse() {
		// 学员编号
		String studentId = form.readString("id");

		String stuId = form.readString("stuId");

		if (StringUtils.isNotBlank(stuId)) {
			studentService.changeStuId(studentId, stuId);
		}

		String courseId = form.readString("courseId");
		String consultantId = form.readString("consultantId");
		String book = form.readString("book");
		String contractId = form.readString("contractId");
		String cardId = form.readString("cardId");
		String title = form.readString("title");
		String lessonCard = form.readString("lessonCard");
		int times = valueReader.name("times").defaultValue(0).intValue();

		Date beginTime = valueReader.name("beginTime").dateValue("yyyy-MM-dd");
		Date endTime = valueReader.name("endTime").dateValue("yyyy-MM-dd");

		Date signTime = valueReader.name("signTime").defaultValue(null).dateValue("yyyy-MM-dd");

		SignCourse signCourse = signCourseService.signUp(studentId, courseId, contractId, cardId, lessonCard, title,
				times, beginTime, endTime, signTime, consultantId, book);

		Student student = signCourse.getStudent();

		setJson(JsonResultBuilder.success(true).msg("提交成功").add("id", student.getId()).json());
		return JSON;
	}

}
